﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Lyon.Utility;

namespace Lyon.Register
{
    public class SimpleRegister
    {
        static string _AppName;
        static RegisterInfo _Info;


        static public void Start(string appName)
        {
            _AppName = appName;
            try
            {
                ReadInfo();
                if (_Info != null)
                {
                    if (_Info.LastVisitDate < DateTime.Now)
                    {
                        _Info.LastVisitDate = DateTime.Now;
                        SaveInfo();
                    }
                    else
                        _Info = null;
                }
            }
            catch
            {
            }
        }
        static public bool Authenticate(string encryptString)
        {
            try
            {
                RegisterInfo info = GetRegisterInfo(_AppName, encryptString);
                if (!CheckRegisterInfo(info))
                {
                    return false;
                }
                _Info = info;
                _Info.IsEnableMachineCode = true;
                _Info.MachineCode = MachineID;
                if (_Info.LastVisitDate < DateTime.Now)
                    _Info.LastVisitDate = DateTime.Now;
                SaveInfo();
                return true;
            }
            catch
            {
                return false;
            }
        }
        static public bool IsAuthenticated
        {
            get
            {
                if (_Info == null)
                {
                    return false;
                }
                return CheckRegisterInfo(_Info);
            }
        }
        static bool CheckRegisterInfo(RegisterInfo info)
        {
            if (info.AppName != _AppName)
            {
                return false;
            }
            if (info.IsEnableMachineCode && info.MachineCode != MachineID)
            {
                return false;
            }
            if (info.IsEnableLimit && info.Limit < DateTime.Now)
            {
                return false;
            }
            return true;
        }

        static void SaveInfo()
        {
            string fileName = String.Format(@"{0}\{1}\{2}", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), _AppName, "license.dat");
            InitDir(Path.GetDirectoryName(fileName));

            string encryptString = GetEncryptString(_AppName, _Info);
            File.WriteAllText(fileName, encryptString);
        }
        static void ReadInfo()
        {
            string fileName = String.Format(@"{0}\{1}\{2}", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), _AppName, "license.dat");
            if (File.Exists(fileName))
            {
                string encryptString = File.ReadAllText(fileName);
                _Info = GetRegisterInfo(_AppName, encryptString);
            }
            else
                _Info = null;
        }
        static void InitDir(string dir)
        {
            if (!Directory.Exists(dir))
            {
                InitDir(Path.GetDirectoryName(dir));
                Directory.CreateDirectory(dir);
            }
        }

        static RegisterInfo GetRegisterInfo(string pwd, string encryptString)
        {
            string license = EncryptUtil.Decrypt(encryptString, pwd);
            return RegisterInfo.GetInfo(license);
        }
        static string GetEncryptString(string pwd, RegisterInfo info)
        {
            string license = RegisterInfo.GetStr(_Info);
            return EncryptUtil.Encrypt(license, pwd);
        }

        
        static public string AppName
        {
            get
            {
                return _AppName;
            }
        }
        static public string LimitString
        {
            get
            {
                if (_Info == null)
                    return null;
                if (!_Info.IsEnableLimit)
                    return "无期";
                return _Info.Limit.ToString();
            }
        }
        static public string UserName
        {
            get
            {
                if (_Info == null)
                    return null;
                return _Info.UserName;
            }
        }
        static public string MachineID
        {
            get
            {
                string processorId = ComputerInfo.Current.ProcessorId;
                string diskId = ComputerInfo.Current.DiskId;
                string macAddress = ComputerInfo.Current.MacAddress;
                return EncryptUtil.Encrypt(processorId + diskId + macAddress, "Register");
            }
        }
    }
}
